package com.five.matebook.utils;

import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateUtils {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private static DateAlias sDateAlias = new DateAlias();

    public static String getNowTime() {
        return toString(new Date());
    }

    public static long getNowTimeLong() {
        return new Date().getTime();
    }
 
    public static long getNowYearBegin() {
        return getNowYearBegin(0);
    }
    public static long getNowYearBegin(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (offset != 0) {
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + offset);
        }
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTimeInMillis();
    }

    public static long getNowYearEnd() {
        return getNowYearEnd(0);
    }
    public static long getNowYearEnd(int offset) {
        return getNowYearBegin(offset + 1) - 1;
    }

    public static long getNowMonthBegin() {
        return getNowMonthBegin(0);
    }
    public static long getNowMonthBegin(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (offset != 0) {
            calendar.add(Calendar.MONTH, offset);
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNowMonthEnd() {
        return getNowMonthEnd(0);
    }
    public static long getNowMonthEnd(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (offset != 0) {
            calendar.add(Calendar.MONTH, offset);
        }
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    public static Date string2Date(int y, int month, int day, int hour, int minite) {
        Calendar c = Calendar.getInstance();
        c.set(y, month, day, hour, minite);
        return c.getTime();
    }

    public static Date string2Date(int y, int month, int day, int hour, int minite, int second) {
        Calendar c = Calendar.getInstance();
        c.set(y, month, day, hour, minite, second);
        return c.getTime();
    }

    public static String toString(Date d) {
        return toString(d, false);
    }
    public static String toString(Date d, boolean yearMonthOnly) {
        if (d != null) {
//            return sdf.format(d);
            return sDateAlias.format(d.getTime(), yearMonthOnly);
        }
        return "";
    }

    public static String toString(long ts) {
        return toString(ts, false);
    }

    /**
     * @param ts
     * @return
     */
    public static String toString(long ts, boolean dateOnly) {
        return sDateAlias.format(ts, dateOnly);
    }

    public static Date string2Date(String date) {
        try {
            // 反向格式化字符串为日期和时间对象
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getAliasList() {
        return sDateAlias.getAliasList();
    }
    public static DateAlias.DateRange getDateRangeByAlias(String alias) {
        return sDateAlias.getDateRangeByAlias(alias);
    }
}
